# # -*- coding: utf-8 -*-
import subprocess


def run_cmd(JMETER_Home, jmx_file, result_file, report_html_dir):
    # 无界面运行JMeter压测命令
    run_cmd = f'cmd.exe /c {JMETER_Home} -n -t {jmx_file} -l {result_file} -e -o {report_html_dir}'

    # 不需要获取屏幕输出是，可以使用os.system()
    # os.system(run_cmd)

    # 需要获取屏幕输出是，可以使用subprocess.Popen()
    output = subprocess.Popen(
        run_cmd,
        shell=False,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
        encoding="gbk"
    )
    while output.poll() is None:
        line = output.stdout.readline()
        line = line.strip()
        if line:
            s1 = str(line)
            print(s1)
    if output.returncode == 0:
        print('Subprogram success')
    else:
        print('Subprogram failed')